package com.app.business.web.business;

import com.app.business.model.ActionHistory;
import com.app.business.model.Contract;
import com.app.business.model.House;
import com.app.business.service.ActionHistoryService;
import com.app.business.service.ContractService;
import com.app.business.service.HouseService;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 操作记录数据管理
 * Created by ddz on 2020/12/9.
 *
 * @author ddz
 */
@Controller
@RequestMapping("/business/actionHistory/*")
@SuppressWarnings("all")
public class ActionHistoryController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private ActionHistoryService actionHistoryService;


    /**
     * 操作记录列表
     */
    @RequestMapping("list")
    @RequiresPermissions("actionHistory:list")
    public void list(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 加载操作记录列表
     *
     * @return
     */
    @RequestMapping("loadList")
    @ResponseBody
    @RequiresPermissions("actionHistory:loadList")
    public Map loadList(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        List<Map> listData = Lists.newArrayList();
        PageBean<ActionHistory> actionHistoryPageBean = this.actionHistoryService.getActionHistoryPageBean(contractId);
        List<ActionHistory> actionHistorieList = actionHistoryPageBean.getDatas();
        Map<String, DataDict> bzMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        if (!CollectionUtils.isEmpty(actionHistorieList)) {
            Map<String, Object> item = null;
            for (ActionHistory actionHistory : actionHistorieList) {
                item = null;
                item = Maps.newHashMap();
                item.put("actionHistoryId", actionHistory.getId());
                item.put("name", StringUtils.getNotNull(actionHistory.getContract().getName()));
                item.put("contractCode", StringUtils.getNotNull(actionHistory.getContract().getContractCode()));
                item.put("zsProjectName", StringUtils.getNotNull(actionHistory.getContract().getZsProject().getProjectName()));
                item.put("doType", StringUtils.getNotNull(actionHistory.getDoType()));
                item.put("doName", StringUtils.getNotNull(actionHistory.getDoName()));
                item.put("doDate", DateUtils.dateToStringDefault(actionHistory.getDoDate()));
                item.put("operation", StringUtils.getNotNull(actionHistory.getOperation()));

                listData.add(item);
            }
        }
        return LayUiUtils.page(actionHistoryPageBean.getTotalCount().intValue(), listData);
    }

}
